﻿define(function (require) {
    return Backbone.View.extend({
        //... is a list tag.
        tagName: "li",
        template: _.template($("#div_project").html()),
        events: {
            "click .projecInfo": "showProjectInfo",
        },
        initialize: function () {
            var $self = this;
            $(this.el).attr('data-theme', 'C')
                      .addClass('ui-btn ui-btn-icon-right ui-li ui-btn-up-c');

            _.bindAll(this, 'render');
            this.model.bind('change', this.render);
            this.model.view = this;
        },
        render: function () {
            $(this.el).html(this.template(this.model.toJSON()));

            var workitem = this.model.get("Workitem");
            var totalCount = workitem.UnfinishedCount +
                             workitem.FinishedCount +
                             workitem.ExpiredCount;


            function getHeightPercent(uf, df) {
                return Math.floor((100 * uf / df) + 1);
            }
            if (totalCount > 0) {
                $(this.el)
                    .find('.txt-workitem-unfinished')
                    .height(getHeightPercent(workitem.UnfinishedCount, totalCount) + "%");
                $(this.el)
                    .find('.txt-workitem-expired')
                    .height(getHeightPercent(workitem.ExpiredCount, totalCount) + "%");
                $(this.el)
                    .find('.txt-workitem-finished')
                    .height(getHeightPercent(workitem.FinishedCount, totalCount) + "%");
            }

            if (this.model.get('DateDiff') > 0)
                $(this.el).find('.txt-diff strong').addClass('txt-delaied');
    
            return this;
        },
        showProjectInfo: function(){
            EventProxy.trigger(EventProxy.Events.showProjectInfo,_.clone(this.model.attributes));
        },
        remove: function () {
            $(this.el).remove();
        },
        clear: function () {

        }
    });
});